<?php
namespace app\common\entity;

use think\Db;
use think\Model;

class Rushgoods extends Model
{
    /**
     * @var string 对应的数据表名
     */
    protected $table = 'rush_order';

    public static function getAllCate()
    {

        return [
            '1' => '公告',
            '2' => '新手解答',
//            '3' => '便民'
        ];
    }
    #搜索商品
    public function hckGoodsLists($category,$keyword,$page,$limit)
    {
        return $this->where('goods_title','like',"%".$keyword."%")->where('category_id',$category)->order('createtime desc')->page($page)->limit($limit)->select();
    }
    /*下拉加载商品*/
    public function hcGoodsLists($category,$page,$limit)
    {
        return $this->where('category_id',$category)->order('createtime desc')->page($page)->limit($limit)->select();
    }
    public function getGoodsLists($page,$limit)
    {
        return $this->order('createtime desc')->page($page)->limit($limit)->select();
    }
    public function hkGoodsLists($keyword,$page,$limit)
    {
        
        return $this->where('goods_title','like',"%".$keyword."%")->order('createtime desc')->page($page)->limit($limit)->select();
    }
    /*
     * 分页商品列表
     * */
    public function getGoodsPages($category_id){

        if($category_id){
            $pageParam['query']['type'] = $category_id;         //type 分类参数
            $data = $this->where('category_id',$category_id)->order('status asc')->paginate(20,false,$pageParam);
        }else{
            $data1 = $this->order('status asc')->select();
            $count = count($data1);
            $data = $this->order('status asc')->paginate(20,$count);
        }

        return $data;
    }
   

    public function getGoodsDetail($goodsId){
        return $this->where(array('id'=>$goodsId))->find();
    }
    public function getUserMobile($user_id){
        return DB::table('user')->where(array('id'=>$user_id))->value('mobile');
    }
    public function getUserName($user_id){
        return DB::table('user')->where(array('id'=>$user_id))->value('nick_name');
    }
    public function addGoods($data)
    {
        $entity = new self();
        $entity->goods_title = $data['title'];
        $entity->goods_content = $data['content'];
        $entity->goods_need_integral = $data['goods_need_integral'];
        $entity->goods_total_need_integral = $data['goods_need_integral']*$data['goods_total_number'];
        $entity->goods_total_number = $data['goods_total_number'];
        $entity->goods_pic = $data['goods_pic'];
        $entity->sort = $data['sort'];
         $entity->stagehao = $data['stagehao'];
        $entity->createtime = time();

        return $entity->save();
    }

    public function updateGoods(Rushgoods $goods, $data)
    {   
        // print_r($data);exit;
        $goods->goods_title = $data['goods_title'];
        $goods->goods_content = $data['goods_content'];
        $goods->goods_need_integral = $data['goods_need_integral'];
        $goods->goods_total_need_integral = $data['goods_need_integral']*$data['goods_total_number'];
        $goods->goods_total_number = $data['goods_total_number'];
        $entity->sort = $data['sort'];
         $goods->stagehao = $data['stagehao'];
        $goods->createtime = time();

        return $goods->save();
    }
    public function updateGoodsSend(Rushgoods $goods, $data)
    {
        $goods->odd_number = $data['odd_number'];
        $goods->odd_company = $data['odd_company'];
        $goods->goods_status = 1;

        return $goods->save();
    }
    /*
     * @param 商品ID
     * 根据商品ID 查询商品图
     * */
    public function getGoodsPic($id){
        $goods = new self();
        $goodsList = $goods->where(array('id'=>$id))->find();
        //提取商品内容中的图片
        $pattern='/<img((?!src).)*src[\s]*=[\s]*[\'"](?<src>[^\'"]*)[\'"]/i';
        preg_match_all($pattern,$goodsList['goods_content'],$out);
        return $out[2];
    }

    /*
     * 添加商品分类
     * */
    public function addGoodsCategoryParent($data){
        $entity = DB::table("goods_category");
        $entity->category_title = $data['category_title'];
        return DB::table("goods_category")->save();
    }

    /*
     * 添加商品分类
     * */
    public function addGoodsCategorySon($data){
        $entity = new self();
        $entity->category_title = $data['category_title'];
        return $entity->save();
    }
}